package no.saiboten.milkywave.controller;

import java.util.UUID;
import java.util.logging.Logger;

import no.saiboten.milkywave.bean.UserModel;
import no.saiboten.milkywave.entity.User;
import no.saiboten.milkywave.graph.GraphService;
import no.saiboten.milkywave.mail.MailSender;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

@Controller
public class UserController {

	Objectify ofy;

	MailSender mailSender;

	GraphService graph;

	private static final Logger LOGGER = Logger.getLogger(UserController.class
			.getName());

	@Autowired
	public UserController(MailSender mailSender, GraphService graph) {
		ofy = ObjectifyService.begin();
		this.mailSender = mailSender;
		this.graph = graph;
	}

	@RequestMapping(value = "/user/create", method = RequestMethod.GET)
	public ModelAndView createUserGET() {
		ModelAndView mav = new ModelAndView("createuser");
		mav.addObject("graph", graph.getGraphImg());
		mav.addObject("graphValues", graph.getValues());
		mav.addObject("model", new UserModel());
		return mav;
	}

	@RequestMapping(value = "/user/create", method = RequestMethod.POST)
	public RedirectView createUser(@ModelAttribute("model") UserModel userModel) {
		LOGGER.info("Adding mail: " + userModel.getMailStr());
		if (userModel.getMailStr() == null) {
			return new RedirectView("/user/create?result=invalidmail");
		}

		User user = new User();
		user.setMail(userModel.getMailStr());
		user.setFirstName(userModel.getFirstName());
		user.setLastName(userModel.getLastName());
		user.setPhoneNumber(userModel.getPhoneNumber());
		user.setDeletionCode(UUID.randomUUID().toString());
		user.setConfirmed(false);
		ofy.put(user);

		mailSender
				.sendMail(
						userModel.getMailStr(),
						"Mailbekreftelse - Milkywave-varsel",
						"Vennligst bekreft e-postaddressen din ved å benytte følgende lenke: http://themilkywave.appspot.com/user/confirm/"
								+ user.getDeletionCode());

		return new RedirectView("/overview?result=usercreated");
	}

	@RequestMapping("/user/confirm/{uuid}")
	public RedirectView confirmUser(@PathVariable("uuid") String uuid) {
		User user = ofy.get(User.class, uuid);
		user.setConfirmed(true);
		ofy.put(user);
		return new RedirectView("/overview?result=userconfirmed");
	}

	@RequestMapping("/delete/{key}")
	public RedirectView deleteUserByUUID(@PathVariable("key") String key) {
		ofy.delete(User.class, key);
		return new RedirectView("/overview?result=userdeleted");
	}

}
